﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using System.Linq.Dynamic.Core;

using KDS.Mes.Mes.TestRlifts.Dtos;
using KDS.Mes.Dto;
using KDS.Mes.Authorization;

#region 代码生成器相关信息_ABP Code Generator Info
//你好，我是ABP代码生成器的作者,欢迎您使用该工具，目前接受付费定制该工具，有需要的可以联系我
//我的邮箱:werltm@hotmail.com
// 官方网站:"http://www.yoyocms.com"
// 交流QQ群：104390185  
//微信公众号：角落的白板报
// 演示地址:"vue版本：http://vue.yoyocms.com angularJs版本:ng1.yoyocms.com"
//博客地址：http://www.cnblogs.com/wer-ltm/
//代码生成器帮助文档：http://www.cnblogs.com/wer-ltm/p/5777190.html
// <Author-作者>梁桐铭 ,微软MVP</Author-作者>
// Copyright © YoYoCms@China.2017-10-24T19:30:02. All Rights Reserved.
//<生成时间>2017-10-24T19:30:02</生成时间>
#endregion


namespace KDS.Mes.Mes.TestRlifts
{
    /// <summary>
    /// 电装检测数据表服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Report_TestRlifts)]
    public class TestRliftAppService : MesAppServiceBase, ITestRliftAppService
    {
        private readonly IRepository<TestRlift, int> _testRliftRepository;
        private readonly ITestRliftListExcelExporter _testRliftListExcelExporter;


        private readonly TestRliftManage _testRliftManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public TestRliftAppService(IRepository<TestRlift, int> testRliftRepository,
TestRliftManage testRliftManage
      , ITestRliftListExcelExporter testRliftListExcelExporter
  )
        {
            _testRliftRepository = testRliftRepository;
            _testRliftManage = testRliftManage;
            _testRliftListExcelExporter = testRliftListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<TestRlift> _testRliftRepositoryAsNoTrack => _testRliftRepository.GetAll().AsNoTracking();


        #endregion


        #region 电装检测数据表管理

        public PagedResultDto<TestRliftListDto> GetPagedTestRlifts(GetTestRliftInput input)
        {
            //初步过滤
            var query = _testRliftRepository.GetAll()
                        .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                        .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                        .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2);
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CREAT_TIME);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList();
            return new PagedResultDto<TestRliftListDto>(tasksCount, taskList.MapTo<List<TestRliftListDto>>());
        }

        /// <summary>
        /// 根据查询条件获取电装检测数据表分页列表
        /// </summary>
        public async Task<PagedResultDto<TestRliftListDto>> GetPagedTestRliftsAsync(GetTestRliftInput input)
        {

            var query = _testRliftRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var testRliftCount = await query.CountAsync();

            var testRlifts = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var testRliftListDtos = testRlifts.MapTo<List<TestRliftListDto>>();
            return new PagedResultDto<TestRliftListDto>(
            testRliftCount,
            testRliftListDtos
            );
        }

        /// <summary>
        /// 通过Id获取电装检测数据表信息进行编辑或修改 
        /// </summary>
        public async Task<GetTestRliftForEditOutput> GetTestRliftForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetTestRliftForEditOutput();

            TestRliftEditDto testRliftEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _testRliftRepository.GetAsync(input.Id.Value);
                testRliftEditDto = entity.MapTo<TestRliftEditDto>();
            }
            else
            {
                testRliftEditDto = new TestRliftEditDto();
            }

            output.TestRlift = testRliftEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取电装检测数据表ListDto信息
        /// </summary>
        public async Task<TestRliftListDto> GetTestRliftByIdAsync(EntityDto<int> input)
        {
            var entity = await _testRliftRepository.GetAsync(input.Id);

            return entity.MapTo<TestRliftListDto>();
        }







        /// <summary>
        /// 新增或更改电装检测数据表
        /// </summary>
        public async Task CreateOrUpdateTestRliftAsync(CreateOrUpdateTestRliftInput input)
        {
            if (input.TestRliftEditDto.Id.HasValue)
            {
                await UpdateTestRliftAsync(input.TestRliftEditDto);
            }
            else
            {
                await CreateTestRliftAsync(input.TestRliftEditDto);
            }
        }

        /// <summary>
        /// 新增电装检测数据表
        /// </summary>
        public virtual async Task<TestRliftEditDto> CreateTestRliftAsync(TestRliftEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<TestRlift>();

            entity = await _testRliftRepository.InsertAsync(entity);
            return entity.MapTo<TestRliftEditDto>();
        }

        /// <summary>
        /// 编辑电装检测数据表
        /// </summary>
        public virtual async Task UpdateTestRliftAsync(TestRliftEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _testRliftRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _testRliftRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除电装检测数据表
        /// </summary>
        public async Task DeleteTestRliftAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _testRliftRepository.DeleteAsync(input.Id);
        }

        #endregion
        #region 电装检测数据表的Excel导出功能


        public async Task<FileDto> GetTestRliftToExcel(GetTestRliftInput input)
        {
            var entities = await _testRliftRepository.GetAll()
                 .WhereIf(!input.SeatID.IsNullOrEmpty(), u => u.SEAT_ID == input.SeatID)
                        .WhereIf(!input.LineId.IsNullOrEmpty(), u => u.LINE == input.LineId)
                        .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.CREAT_TIME >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.CREAT_TIME <= input.Time2)
                .ToListAsync();

            var dtos = entities.MapTo<List<TestRliftListDto>>();

            var fileDto = _testRliftListExcelExporter.ExportTestRliftToFile(dtos);



            return fileDto;
        }


        #endregion










    }
}
